<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list " :key="item.id" :label="item.id"> {{ item.name }} </el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/role'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employess'
export default {
  props: {
    // 用户的id 用来查询当前用户的角色信息
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.initrole()
    this.getuserID()
  },
  methods: {
    closeDialog() {
      this.$emit('close')
    },
    async initrole() {
      const res = await getRoles()
      this.list = res.data.rows
    },
    async getuserID() {
      const res = await getUserDetailById(this.id)
      console.log(res)
      // 复选框的选中状态等于数组里面的roleIds
      this.roleIds = res.data.roleIds
    },
    async hSubmit() {
      await assignRoles({ id: this.id, roleIds: this.roleIds })
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
